package com.hdoit.apollo.web.shop.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.apollo.logical.shop.model.Shop;
import com.hdoit.apollo.logical.shop.model.Supplier;
import com.hdoit.apollo.logical.shop.service.ISupplierService;
import com.hdoit.apollo.logical.viewmodel.shop.SupplierSearch;
import com.tennetcn.authority.component.web.ActionResult;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.ErrorResult;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.core.common.utils.JsonUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年05月22日 10:09:14
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/supplier/")
public class SupplierController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/supplier/";
    }

    @Autowired
    private ISupplierService supplierService;

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="supplierDetailed";
    private final String saveViewName="save";
    private final String savesViewName="saves";
    private final String deleteViewName = "delete";
    
    private final String quickAddViewName="quickAdd";
    
    private final String supplierListViewName="supplierList";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,SupplierSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(supplierService.queryCountMP(search));
        List<Supplier> suppliers= supplierService.queryListMP(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("suppliers", suppliers);
        model.addAttribute("supplierSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = quickAddViewName, method = { RequestMethod.GET})
    public String upCargos(Model model,String key){
    	 Supplier supplier=new Supplier();
         if(!StringUtils.isEmpty(key)){
             supplier=supplierService.queryModel(key);
         }
         model.addAttribute("supplier", supplier);
         return view(quickAddViewName);
    }
    
    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String supplierDetailed(Model model,String key){
        Supplier supplier=new Supplier();
        if(!StringUtils.isEmpty(key)){
            supplier=supplierService.queryModel(key);
        }
        model.addAttribute("supplier", supplier);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Supplier supplier){
    	
    	Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
        if(StringUtils.isEmpty(supplier.getSupplierId())){
            supplier.setSupplierId(CommonUtils.getKeyId());
            supplier.setCreateDate(DateUtils.getCurrent());
            supplier.setDeleteMark(YesOrNoInteger.no);
            supplier.setCreateUserId(getLoginModel().getLoginId());
            supplier.setCreateUserName(getLoginModel().getLoginName());
            supplier.setShopId(shop.getShopId());
            supplier.setModelStatus(ModelStatus.add);
        }else{
            supplier.setModifyDate(DateUtils.getCurrent());
            supplier.setModifyUserId(getLoginModel().getLoginId());
            supplier.setModifyUserName(getLoginModel().getLoginName());
            supplier.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(supplierService.applyChange(supplier)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("supplier", supplier);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }
    
    
    @ResponseBody
    @RequestMapping(value = savesViewName, method = { RequestMethod.POST },produces = "text/json;charset=UTF-8")
    public String saves(@RequestBody List<Supplier> suppliers){
        
    	
    	for (Supplier supplier : suppliers) {
    		supplier.setSupplierId(CommonUtils.getKeyId());
    		supplier.setCreateDate(DateUtils.getCurrent());
    		supplier.setDeleteMark(YesOrNoInteger.no);
    		supplier.setCreateUserId(getLoginModel().getLoginId());
    		supplier.setCreateUserName(getLoginModel().getLoginName());
    		supplier.setModelStatus(ModelStatus.add);
            Shop shop=(Shop)webSessionFactory.getCurrentLogin().getArgument("shop");
            supplier.setShopId(shop.getShopId());
           
        }

        if(supplierService.saveSuppliers(suppliers)){
        	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }else{
        	return new JsonActionResult(ManageContainer.ERROR).toJson();
        }

    }
    
    @RequestMapping(value = supplierListViewName, method = { RequestMethod.GET }, produces = "text/html;charset=UTF-8")
   	public @ResponseBody String supplierList(PagerModel pagerModel,SupplierSearch search) {

    	 List<Supplier> suppliers=supplierService.queryListMP(search, null);
   		
   		return JsonUtils.writeValueAsString(suppliers);
   	}

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(supplierService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}